package cn.bytecollege.controller;

import cn.bytecollege.entity.Room;
import cn.bytecollege.service.RoomCategoryService;
import cn.bytecollege.service.RoomService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "RoomCategoryDeleteController",value = "/roomCategoryDelete")
public class RoomCategoryDeleteController extends HttpServlet {

    private RoomCategoryService roomCategoryService=null;
    private RoomService roomService=null;

    public RoomCategoryDeleteController(){
        roomCategoryService=new RoomCategoryService();
        roomService=new RoomService();
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String msg=null;
        //接收参数
        String id=request.getParameter("id");
        Integer roomCategoryId=Integer.valueOf(id);
        //判断当前要删除的分类是否被引用
        List<Room> list=roomService.findRoomCategoryId(roomCategoryId);
        if(list.size()>0){
            msg="当前分类已被引用，不能删除！";
            request.setAttribute("msg",msg);
            request.getRequestDispatcher("/roomCategoryList").forward(request,response);
            return;
        }
        //调用service方法
        roomCategoryService.deleteCategory(roomCategoryId);
        //跳转页面
        response.sendRedirect(request.getContextPath()+"/roomCategoryList");
    }
}
